package Persistencia;

import Negocio.Cliente;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;

public class ClienteDAO extends BaseDAO {

    private PreparedStatement stIncluir;
    private PreparedStatement stLista;
    private PreparedStatement stRemover;
    private PreparedStatement stConsulta;
    private PreparedStatement stConsultaNum;
    private PreparedStatement stTroca;

    public ClienteDAO() throws IOException {
        super();
        Connection con = getConexao();
        try {
            stIncluir = con.prepareStatement("INSERT INTO cliente (nome,cpf,cnh,endereco,cidade) VALUES (?,?,?,?,?)");
            stLista = con.prepareStatement("SELECT * FROM cliente order by numero");
            stRemover = con.prepareStatement("DELETE * FROM cliente WHERE nomeC=?");
            stConsulta = con.prepareStatement("SELECT * FROM cliente WHERE nome ilike ? ");
            stConsultaNum = con.prepareStatement("SELECT * FROM cliente WHERE numero= ? ");
            stTroca = con.prepareStatement("UPDATE cliente set nome=?,cpf=?,cnh=?,endereco=?,cidade=? where numero=?");
        } catch (SQLException e) {
            throw new IOException("Nao foi possivel criar as consultas: " + e.getMessage());
        }
    }

    public void incluir(Cliente c) throws IOException {
        try {

            stIncluir.setString(1, c.getNome());
            stIncluir.setString(2, c.getCpf());
            stIncluir.setString(3, c.getCnh());
            stIncluir.setString(4, c.getEndereco());
            stIncluir.setString(5, c.getCidade());
            stIncluir.executeUpdate();
        } catch (SQLException e) {
            throw new IOException("Erro ao incluir O/: " + e.getMessage());
        }
    }

    public ArrayList<Cliente> consulta() throws IOException {
        ArrayList<Cliente> tmp = new ArrayList<Cliente>();
        try {
            ResultSet rs = stLista.executeQuery();
            while (rs.next()) {
                tmp.add(new Cliente(rs.getInt("numero"),rs.getString("nome"), rs.getString("cpf"), rs.getString("endereco"), rs.getString("cidade")));
            }
        } catch (SQLException e) {
            throw new IOException("Erro ao consultar: " + e.getMessage());
        }
        return tmp;
    }

    public ArrayList<Cliente> consultaPorNome(String nome) throws IOException {
        ArrayList<Cliente> tmp = new ArrayList<Cliente>();
        try {
            stConsulta.setString(1, nome + "%");

            ResultSet rs = stConsulta.executeQuery();

            while (rs.next()) {
                tmp.add(new Cliente(rs.getString("nome"), rs.getString("cpf"),rs.getString("cnh"), rs.getString("endereco"), rs.getString("cidade")));
            }
        } catch (SQLException e) {
            throw new IOException("Erro ao consultar: " + e.getMessage());
        }

        return tmp;
    }

    
     public ArrayList<Cliente> consultaPorNumero(int numero) throws IOException {
        ArrayList<Cliente> tmp = new ArrayList<Cliente>();
        try {
            stConsultaNum.setInt(1, numero);

            ResultSet rs = stConsultaNum.executeQuery();

            while (rs.next()) {
                tmp.add(new Cliente(rs.getInt("numero")+rs.getString("nome"), rs.getString("cpf"),rs.getString("cnh"), rs.getString("endereco"), rs.getString("cidade")));
            }
        } catch (SQLException e) {
            throw new IOException("Erro ao consultar: " + e.getMessage());
        }

        return tmp;
    }
      public void trocar(Cliente c) throws IOException {
        try {

            stTroca.setString(1, c.getNome());
            stTroca.setString(2, c.getCpf());
            stTroca.setString(3, c.getEndereco());
            stTroca.setString(4, c.getCidade());
            stTroca.setInt(5, c.getNumero());
            stTroca.executeUpdate();
        } catch (SQLException e) {
            throw new IOException("Erro ao alterar : " + e.getMessage());
        }
    }
}
